#include "bintree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

BinTreeNode* bintreeCreate(const char* skill) {
    BinTreeNode* node = (BinTreeNode*)malloc(sizeof(BinTreeNode));
    if (!node) return NULL;
    strncpy(node->skill, skill, SKILL_NAME_LEN-1);
    node->skill[SKILL_NAME_LEN-1] = '\0';
    node->left = node->right = NULL;
    return node;
}

void bintreeInsertLeft(BinTreeNode* parent, BinTreeNode* child) {
    if (parent) parent->left = child;
}

void bintreeInsertRight(BinTreeNode* parent, BinTreeNode* child) {
    if (parent) parent->right = child;
}

void bintreeFree(BinTreeNode* root) {
    if (!root) return;
    bintreeFree(root->left);
    bintreeFree(root->right);
    free(root);
}

void bintreePreOrder(BinTreeNode* root, void (*visit)(BinTreeNode*)) {
    if (!root) return;
    visit(root);
    bintreePreOrder(root->left, visit);
    bintreePreOrder(root->right, visit);
}

void bintreeInOrder(BinTreeNode* root, void (*visit)(BinTreeNode*)) {
    if (!root) return;
    bintreeInOrder(root->left, visit);
    visit(root);
    bintreeInOrder(root->right, visit);
}

void bintreePostOrder(BinTreeNode* root, void (*visit)(BinTreeNode*)) {
    if (!root) return;
    bintreePostOrder(root->left, visit);
    bintreePostOrder(root->right, visit);
    visit(root);
}

int bintreeDepth(BinTreeNode* root) {
    if (!root) return 0;
    int l = bintreeDepth(root->left);
    int r = bintreeDepth(root->right);
    return (l > r ? l : r) + 1;
}

int bintreeNodeCount(BinTreeNode* root) {
    if (!root) return 0;
    return 1 + bintreeNodeCount(root->left) + bintreeNodeCount(root->right);
}

int bintreeLeafCount(BinTreeNode* root) {
    if (!root) return 0;
    if (!root->left && !root->right) return 1;
    return bintreeLeafCount(root->left) + bintreeLeafCount(root->right);
} 